package com.lingyuan.common.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName：ThreadPoolExecutorConfiguration
 * @Author: LeiHao
 * @Date: 2024/8/26 20:39
 * @Description: order 自定义线程池配置
 */
@Configuration //交给spring管理
public class ThreadPoolExecutorConfiguration {


    /**
     * 订单线程池
     * @return
     */
    @Bean("orderThreadPool")
    public ThreadPoolTaskExecutor orderThreadPool() {
        //nThreads 计算了线程池的核心线程数和最大线程数，
        // 等于系统可用处理器数量的两倍。
        int nThreads = 2 * Runtime.getRuntime().availableProcessors();
        //阻塞队列容器就是核心线程数的30倍
        int blockingQueueSize = nThreads * 30;
        //这里没有声明拒绝策略,采用的默认拒绝策略,抛异常
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(nThreads); // 核心线程数
        executor.setMaxPoolSize(nThreads); // 最大线程数
        executor.setQueueCapacity(blockingQueueSize); // 队列大小
        executor.setThreadNamePrefix("OrderThreadPool-"); // 线程名前缀
        executor.initialize();
        return executor;
    }
}
